package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BaseSpaceLocation;
import com.ruoyi.system.service.IBaseSpaceLocationService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 基础信息空间位置Controller
 *
 * @author ruoyi
 * @date 2023-03-29
 */
@RestController
@RequestMapping("/system/location")
public class BaseSpaceLocationController extends BaseController {
    @Autowired
    private IBaseSpaceLocationService baseSpaceLocationService;

    /**
     * 查询基础信息空间位置列表
     */
    @PreAuthorize("@ss.hasPermi('system:location:list')")
    @GetMapping("/list")
    public AjaxResult list(BaseSpaceLocation baseSpaceLocation) {
        List<BaseSpaceLocation> list = baseSpaceLocationService.selectBaseSpaceLocationList(baseSpaceLocation);
        return AjaxResult.success(list);
    }

    /**
     * 导出基础信息空间位置列表
     */
    @PreAuthorize("@ss.hasPermi('system:location:export')")
    @Log(title = "基础信息空间位置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseSpaceLocation baseSpaceLocation) {
        List<BaseSpaceLocation> list = baseSpaceLocationService.selectBaseSpaceLocationList(baseSpaceLocation);
        ExcelUtil<BaseSpaceLocation> util = new ExcelUtil<BaseSpaceLocation>(BaseSpaceLocation.class);
        util.exportExcel(response, list, "基础信息空间位置数据");
    }

    /**
     * 获取基础信息空间位置详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:location:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(baseSpaceLocationService.selectBaseSpaceLocationById(id));
    }

    /**
     * 新增基础信息空间位置
     */
    @PreAuthorize("@ss.hasPermi('system:location:add')")
    @Log(title = "基础信息空间位置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseSpaceLocation baseSpaceLocation) {
        // 根据上级位置去查
        BaseSpaceLocation location = baseSpaceLocationService.selectBaseSpaceLocationById(baseSpaceLocation.getLocationPid());
        // 拼接层级名称
        baseSpaceLocation.setLevelName(location.getLocationName() + "-" + baseSpaceLocation.getLocationName());
        // 得到父层级
        Integer pLevel = Integer.valueOf(location.getLevel());
        // 在父层级的基础上 + 1
        Integer level = pLevel + 1;
        //得到层级
        baseSpaceLocation.setLevel(level.toString());
        // 状态
        baseSpaceLocation.setStatus("1");
        return toAjax(baseSpaceLocationService.insertBaseSpaceLocation(baseSpaceLocation));
    }

    /**
     * 修改基础信息空间位置
     */
    @PreAuthorize("@ss.hasPermi('system:location:edit')")
    @Log(title = "基础信息空间位置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseSpaceLocation baseSpaceLocation) {
        return toAjax(baseSpaceLocationService.updateBaseSpaceLocation(baseSpaceLocation));
    }

    /**
     * 删除基础信息空间位置
     */
    @PreAuthorize("@ss.hasPermi('system:location:remove')")
    @Log(title = "基础信息空间位置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(baseSpaceLocationService.deleteBaseSpaceLocationByIds(ids));
    }
}
